<?php
/**
* @filename source_class.php 2013-11-11 02:10:56x
* @author Huming Xu <huming17@126.com>
* @version 1.0.0
* @copyright DZF (c) 2013, Huming Xu
*/
header("Content-Type: text/html;charset=utf-8");
if(!defined('IN_SITE')) {
	exit('Access Denied');
}
class course_hour_record{
	public $info_array = array();
	public $page_array = array();
	public $tree_array = array();

	static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}
	/*
	*	获取一条信息
	*/
	public function one_info($table_name,$key_name,$key_id='') {	
		$info = array();
		if($table_name && $key_name && $key_id){
			$sql = "SELECT * FROM ".DB::table($table_name)." WHERE ".$key_name."='".$key_id."' LIMIT 1";
			$info = DB::fetch_first($sql);	
		}
		return $info;
	}
    
	/*
	*  添加信息
	*/
	public function add_time($table_name,$update_data=array(),$where) {
        $effect_row='';
        $info = $data_result = array();
        if($table_name && $update_data){  
        	
        	$sql = "SELECT * FROM ".DB::table('course_hour_record')." WHERE ch_id = ".$where['ch_id'] ." and user_id = ".$where['user_id']
        	." and r_key = ".$update_data['key'] ;
        	$is_key = DB::fetch_first($sql);
        	        	
        	$sql = "SELECT * FROM ".DB::table($table_name)." WHERE ch_id = ".$where['ch_id'] ." and user_id = ".$where['user_id'];
        	$info = DB::fetch_first($sql);
        	
            if($info){        
	            if(empty($is_key)){
	            	$update_data['order_num'] = $info['order_num'] + 1;
	            }
	            $update_data['add_time'] +=$info['add_time']; 
            }
            //插入播放记录
            $sql = "SELECT ch.cm_id,ch.ch_teacher_id,cm.cm_subject_id FROM ".DB::table('course_hour')."  as ch
        				LEFT JOIN ".DB::table('course_main')." cm  ON ch.cm_id=cm.cm_id
						WHERE ch.ch_id =  ".$update_data['ch_id'];
            $cm_data = DB::fetch_first($sql);
            
            $sql = "SELECT * FROM ".DB::table('user_detail')."
						WHERE user_id =  ".$update_data['user_id'];
            $user_data= DB::fetch_first($sql);
            
            $insert_data=array(
            		"tcr_id" =>$cm_data['ch_teacher_id'],
            		"ch_id" =>$update_data['ch_id'],
            		"cm_id" =>$cm_data['cm_id'],
            		"cm_subject_id"=>$cm_data['cm_subject_id'],
            		"user_id" =>$update_data['user_id'],
            		"ue_area_id1"=>$user_data['am_id_1'],
            		"ue_area_id2"=>$user_data['am_id_2'],
            		"ue_area_id3"=>$user_data['am_id_3'],
            		"so_id"      =>$user_data['so_id'],
            		"sm_id"      =>$user_data['sm_id'],
            		"grade_id"   =>$user_data['grade_num'],
            		"class_id"	 =>$user_data['class_id'],            		
            		"add_time"=> $update_data['add_time'],
            		"dateline" =>$update_data['modify_dateline'],           		
            		"modify_dateline" =>$update_data['modify_dateline'],
            		"r_key"       => $update_data['key']
            );
            $insert_course_hour_record = DB::insert('course_hour_record',$insert_data,true);
                      
            //在统计表中更新或插入数据
            unset($update_data['key']);
        	$effect_row = DB::update($table_name,$update_data,$where);
        	if(!$effect_row){        		
        		//DB::update($table_name,array("modify_dateline"=>$update_data['modify_dateline']),$where);       	      
        		$insert_data=array(
        			"tcr_id" =>$cm_data['ch_teacher_id'],	
        			"ch_id" =>$update_data['ch_id'],
        			"cm_id" =>$cm_data['cm_id'],
        			"cm_subject_id"=>$cm_data['cm_subject_id'],	
        			"user_id" =>$update_data['user_id'],
        			"ue_area_id1"=>$user_data['am_id_1'],
        			"ue_area_id2"=>$user_data['am_id_2'],
        			"ue_area_id3"=>$user_data['am_id_3'],
        			"so_id"      =>$user_data['so_id'],
        			"sm_id"      =>$user_data['sm_id'],
        			"grade_id"   =>$user_data['grade_num'],
        			"class_id"	 =>$user_data['class_id'],
        			"order_num"  =>'1',	
        			"add_time"=>$update_data['add_time'],
        			"dateline" =>$update_data['modify_dateline'],
        			"modify_dateline" =>$update_data['modify_dateline']	
        		);
        		$insert_id = DB::insert($table_name,$insert_data,true,true);       		
        		if($insert_id){
        			return true;
        		}
        	}
        } 
       
        return $effect_row;
	}
    
	/*
	*  编辑信息
	*/
	public function edit($table_name,$update_data,$where) {
        //DEBUG 定义返回 消息格式与消息代码编号
        $effect_row='';
        if($update_data && $where){
            $effect_row = DB::update($table_name,$update_data,$where);
        }
        return $effect_row;
	}
	
	/*
	 *  编辑播放记录信息
	 */
	public function edit_hour_record($table_name,$update_data,$where) {
		//DEBUG 定义返回 消息格式与消息代码编号
		$effect_row='';
		if($update_data && $where){
			$effect_row = DB::update($table_name,$update_data,$where);
		}
		if(!$effect_row){
			
			$sql = "SELECT cm_id,ch_teacher_id FROM ".DB::table('course_hour')."
						WHERE ch_id =  ".$update_data['ch_id'];
			
			$cm_id = DB::fetch_first($sql);
			
			//学科
			if($cm_id['cm_id']){
				$sql = "SELECT cm_subject_id,so_id FROM ".DB::table('course_main')."
						WHERE cm_id =  ".$cm_id['cm_id'];
				$cm_data = DB::fetch_first($sql);
			}else{
				$cm_data =array(					
					'cm_subject_id'=>'',
					'so_id'=>''	
				);
			}
			//用户详情
			$user_detail =$this->one_info('user_detail','user_id',$update_data['user_id']);
			
			$insert_data=array(
					"tcr_id" => $cm_id['ch_teacher_id'],//主讲老师编号
					"ch_id" =>$update_data['ch_id'],//课时
					"cm_id" =>$cm_id['cm_id'],//课程										
					"user_id" =>$update_data['user_id'],//上课用户
					"ue_area_id1" => $user_detail['am_id_1'],//省
					"ue_area_id2" => $user_detail['am_id_2'],//市
					"ue_area_id3" => $user_detail['am_id_3'],//县
					"so_id"       => $user_detail['so_id'],//网校编号
					"sm_id"       => $user_detail['sm_id'],//学校编号
					"grade_id"    => $user_detail['grade_num'],//年级编号
					"class_id"    => $user_detail['class_id'],//班级编号
					"cm_subject_id" => $cm_data['cm_subject_id'],//学科
					"look_time"=>$update_data['look_time'],
					"order_num"  =>'1',
					"dateline" => $update_data['modify_dateline'],
					"modify_dateline" =>$update_data['modify_dateline']
			);							
			$insert_id = DB::insert($table_name,$insert_data,true,true);
			if($insert_id){
				return true;
			}
		}
		return $effect_row;
	}
	
	/*
	 *  设播放完成
	 */
	public function edit_is_looked($table_name,$update_data,$where) {
		//DEBUG 定义返回 消息格式与消息代码编号
		$effect_row='';
		if($update_data && $where){
			/* $sql = "SELECT * FROM ".DB::table($table_name)."
						WHERE ch_id =  ".$where['ch_id']." and user_id = ".$where['user_id'];
			$info = DB::fetch_first($sql);
			$update_data['order_num'] = $info['order_num'] + 1; */
			
			//插入原始记录			
			$sql = "SELECT ch.cm_id,ch.ch_teacher_id,cm.cm_subject_id FROM ".DB::table('course_hour')."  as ch
        				LEFT JOIN ".DB::table('course_main')." cm  ON ch.cm_id=cm.cm_id
						WHERE ch.ch_id =  ".$update_data['ch_id'];
			$cm_data = DB::fetch_first($sql);
			
			$sql = "SELECT * FROM ".DB::table('user_detail')."
						WHERE user_id =  ".$update_data['user_id'];
			$user_data= DB::fetch_first($sql);
			
			$insert_data=array(
					"tcr_id" =>$cm_data['ch_teacher_id'],
					"ch_id" =>$update_data['ch_id'],
					"cm_id" =>$cm_data['cm_id'],
					"cm_subject_id"=>$cm_data['cm_subject_id'],
					"user_id" =>$update_data['user_id'],
					"ue_area_id1"=>$user_data['am_id_1'],
					"ue_area_id2"=>$user_data['am_id_2'],
					"ue_area_id3"=>$user_data['am_id_3'],
					"so_id"      =>$user_data['so_id'],
					"sm_id"      =>$user_data['sm_id'],
					"grade_id"   =>$user_data['grade_num'],
					"class_id"	 =>$user_data['class_id'],				
					"is_looked"=>$update_data['is_looked'],
					"dateline" =>$update_data['modify_dateline'],
					"modify_dateline" =>$update_data['modify_dateline'],
					"r_key"       => $update_data['key']
			);
			$insert_course_hour_record = DB::insert('course_hour_record',$insert_data,true);
			
			unset($update_data['key']);
			$effect_row = DB::update($table_name,$update_data,$where);
		}
		if(!$effect_row){
			
			$sql = "SELECT cm_id,ch_teacher_id FROM ".DB::table('course_hour')."
						WHERE ch_id =  ".$update_data['ch_id'];
				
			$cm_id = DB::fetch_first($sql);
				
			//学科
			if($cm_id['cm_id']){
				$sql = "SELECT cm_subject_id,so_id FROM ".DB::table('course_main')."
						WHERE cm_id =  ".$cm_id['cm_id'];
				$cm_data = DB::fetch_first($sql);
			}else{
				$cm_data =array(
						'cm_subject_id'=>'',
						'so_id'=>''
				);
			}
			//用户详情
			$user_detail =$this->one_info('user_detail','user_id',$update_data['user_id']);
				
			$insert_data=array(
					"tcr_id" => $cm_id['ch_teacher_id'],//主讲老师编号
					"ch_id" =>$update_data['ch_id'],//课时
					"cm_id" =>$cm_id['cm_id'],//课程
					"user_id" =>$update_data['user_id'],//上课用户
					"ue_area_id1" => $user_detail['am_id_1'],//省
					"ue_area_id2" => $user_detail['am_id_2'],//市
					"ue_area_id3" => $user_detail['am_id_3'],//县
					"so_id"       => $cm_data['so_id'],//网校编号
					"sm_id"       => $user_detail['sm_id'],//学校编号
					"grade_id"    => $user_detail['grade_num'],//年级编号
					"class_id"    => $user_detail['class_id'],//班级编号
					"cm_subject_id" => $cm_data['cm_subject_id'],//学科
					"is_looked"=>$update_data['is_looked'],
					"look_time"=>0,
					"order_num"  =>'0',
					"modify_dateline" =>$update_data['modify_dateline']
			);
			
			$insert_id = DB::insert($table_name,$insert_data,true);
			if($insert_id){
				return true;
			}
		}
		return $effect_row;
	}
	
	/*
	 *  判断课时学习是否到时间可以进入
	 */
	public function is_start($table_name,$data,$where) {
		$is_start = false;		
		//DEBUG 定义返回 消息格式与消息代码编号				
		$sql = "SELECT cm_id,ch_start_time FROM ".DB::table('course_hour')."
				WHERE isdelete = 0 and ch_id =  ".$data['ch_id'];
		$ch_data = DB::fetch_first($sql);
		if($ch_data['cm_id']){
			$sql = "SELECT  tc_id FROM ".DB::table('trade_course')."
					WHERE isdelete = 0 and tc_flag =2 and cm_id =  ".$ch_data['cm_id']." and user_id = ".$data['user_id'];
			$tc_id = DB::fetch_first($sql);			
			//已购买该课程
			if($tc_id['tc_id']){
				if($data['time']>$ch_data['ch_start_time']||$ch_data['ch_start_time']==''){
					$is_start = true;
				}
			}
		}				
		return $is_start;
	}
	
	
	
	
	/*
	 *  进入课堂相关
	 */
	public function enter_info($table_name,$data,$where) {
		$res_data = array();
		//课程是否购买
		$sql = "SELECT  tc_id FROM ".DB::table('trade_course')."
					WHERE isdelete = 0 and tc_flag =2 and cm_id =  ".$data['cm_id']." and user_id = ".$data['user_id'];
		$tc_id = DB::fetch_first($sql);		
		//已购买该课程
		if($tc_id['tc_id']){
			if($data['ch_id']){//传入了课时ID
				$sql = "SELECT ch_id,ch_type,ch_start_time FROM ".DB::table('course_hour')."
							WHERE isdelete = 0 and ch_id =  ".$data['ch_id']." and cm_id = ".$data['cm_id'];
				$ch_data = DB::fetch_first($sql);
				
				if($ch_data['ch_start_time']<TIMESTAMP||$ch_data['ch_start_time']==''){
					unset($ch_data['ch_start_time']);
					$res_data=$ch_data;
				}else{
					$res_data=['ch_id'=>'','ch_type'=>'','url'=>''];
				}
								
			}else{
				//当前时间是否有直播课程
				$sql = "SELECT ch_id,ch_type FROM ".DB::table('course_hour')."
							WHERE isdelete = 0 and cm_id =  ".$data['cm_id']." and ch_type = 1 
							and ch_start_time <= ".TIMESTAMP." order by ch_sort";
				$is_live = DB::fetch_first($sql);
				if($is_live){
					$res_data=$is_live;
				}else{					
					//看到第几课时			
					$sql = "SELECT distinct(ch.ch_id),ch.ch_type,ch.ch_vod_url,ch.ch_start_time FROM ".DB::table('course_hour_looked')." chr
						LEFT JOIN ".DB::table('course_hour')." ch ON  ch.ch_id = chr.ch_id 
						WHERE 1=1  ".$where. " order by chr.modify_dateline  desc";				
					$record_data = DB::fetch_first($sql);
					
					if($record_data){
						$record_data['url']= $record_data['ch_vod_url'];
						unset($record_data['ch_vod_url']);
						$res_data=$record_data;
					}else{
						//取第一课时
						
						//选取课程下的第一个课时
						$ch_data = $this->get_cm_data($data['cm_id'],$data['user_id'],true);
						
						if($ch_data['ch_start_time']<TIMESTAMP||$ch_data['ch_start_time']==''){
							$res_data=['ch_id'=>$ch_data['ch_id'],'ch_type'=>$ch_data['ch_type'],'url'=>$ch_data['best_url']];
						}else{
							$res_data=['ch_id'=>'','ch_type'=>'','url'=>''];
						}
											
					}
				}
			}
			
		}
		/* if($res_data){			
			$data['ch_id'] = $res_data['ch_id'];
			$this->add_study_info($data);
		} */
		return $res_data;
	}
	
	/*
	 *  添加学习记录
	 */
	function add_study_info($data){
		$ch_teacher_id=$cm_subject_id=$so_id='';
		if($data['ch_id']){
			$sql = "SELECT ch_teacher_id FROM ".DB::table('course_hour')."
					WHERE ch_id =  ".$data['ch_id'];				
			$ch_data = DB::fetch_first($sql);
			$ch_teacher_id=$ch_data['ch_teacher_id'];
		}
		if($data['cm_id']){
			//学科			
			$sql = "SELECT cm_subject_id,so_id FROM ".DB::table('course_main')."
						WHERE cm_id =  ".$data['cm_id'];
			$cm_data = DB::fetch_first($sql);
			$cm_subject_id = $cm_data['cm_subject_id'];
			$so_id= $cm_data['so_id'];
		}
		//用户详情
		$user_detail =$this->one_info('user_detail','user_id',$data['user_id']);			
		$insert_data=array(
				"tcr_id" => $ch_teacher_id,//主讲老师编号
				"ch_id" =>$data['ch_id'],//课时
				"cm_id" =>$data['cm_id'],//课程
				"user_id" =>$data['user_id'],//上课用户
				"ue_area_id1" => $user_detail['am_id_1'],//省
				"ue_area_id2" => $user_detail['am_id_2'],//市
				"ue_area_id3" => $user_detail['am_id_3'],//县
				"so_id"       => $so_id,//网校编号
				"sm_id"       => $user_detail['sm_id'],//学校编号
				"grade_id"    => $user_detail['grade_num'],//年级编号
				"class_id"    => $user_detail['class_num'],//班级编号
				"cm_subject_id" => $cm_subject_id,//学科			
				"start_time" =>TIMESTAMP
		);		 
		$insert_id = DB::insert('course_hour_record',$insert_data,true);		
	}
    
	/*
	*  删除信息
	*/
	public function delete($table_name,$where) {
        $return='';
        if($table_name && $where){
            //$return = DB::delete($table_name, $where, $limit=1);
            //TODO 逻辑删需要创建isdelete字段 0=未删除 1=已删除       	
            $return=DB::update($table_name,array("isdelete"=>1),$where, $limit=1);
        }
        return $return;
    }
	/*
	*  列表信息
    *
    * @param $table_name string.
    * @param $page_condition array.
    *
    * @return array
	*/
	public function index($table_name,$page_condition){
        $page_result = $data_result = array();
        $page = $page_condition['page'];
        $limit = $page_condition['limit'];
        $perpage = $page_condition['perpage'];
        $start=(($page-1) * $perpage);
        $wheresql = $page_condition['wheresql'];
        $orderby = $page_condition['orderby'];
       /*  $sql_info = "SELECT * FROM ".DB::table($table_name)." WHERE 1=1 ".$wheresql." ".$page_condition['orderby']." ".DB::limit($start, $limit);
        $sql_info_result = DB::fetch_all($sql_info); */
        $sql = "SELECT cm.cm_name,cn.cn_id,cn.cn_content,cn.modify_dateline,cn.user_id,cn.user_id,cn.cn_dateline,cn.create_dateline,ch.ch_name FROM ".DB::table('course_note')."  cn
				LEFT JOIN " . DB::table('course_main') . "  cm ON cn.cm_id = cm.cm_id
				LEFT JOIN " . DB::table('course_hour') . "  ch ON ch.ch_id = cn.ch_id
				WHERE 1=1 ".$wheresql." ".$page_condition['orderby']." ".DB::limit($start, $limit);
        $info = DB::fetch_all($sql);
        	
        foreach($info AS $key => $value){
        	$tmp = array();
        	$tmp['id']=$value['cn_id'];
        	$tmp['content']=$value['cn_content'];
        	$tmp['course']=$value['cm_name'];
        	$tmp['chapter']=$value['ch_name'];
        	$tmp['cn_dateline']=$value['cn_dateline'];
        	$tmp['create_dateline']=date("Y-m-d H:i:s",$value['create_dateline']);
        	$tmp['modify_dateline']=date("Y-m-d H:i:s",$value['modify_dateline']);
        	$tmp['user_id']=$value['user_id'];
        	$tmp['month']=date("m",$value['modify_dateline']);
        	$tmp['date']=date("d",$value['modify_dateline']);
        	$tmp['hour']=date("h:i",$value['modify_dateline']);
        	$data_result[]=$tmp;
        }
        
        $sql_total_rows = "SELECT count(*) FROM ".DB::table($table_name)." cn WHERE 1=1 ".$wheresql."";
        $sql_total_rows_result = DB::result_first($sql_total_rows);
        //DEBUG 列表数据返回结构
        $page_result = array(
            //int 返回结果总数
            'total_rows' => $sql_total_rows_result,
            //array 一页数据数组
            'page_data' => $data_result 
        );
        return $page_result;
	}
	
	/*
	 *  点击继续学习详情
	 *
	 * 
	 * @param $id int.
	 *
	 * @return array
	 */
	/* public function get_continue_detail($id,$user_id){
		if(!$id||!$user_id){
			return array();
		}
		
		$all_duration = $all_look_time = 0;
		$ch_res = $now_data = $cm_data = $ip_data= $operators = $other_ip=$other_url=array();
		$ch_operators = $other_ch_ip = $other_url=array();
		$bast_url = '';
		//获取课程信息
		$sql = "SELECT ch_id,cm_id,look_time FROM ".DB::table('course_hour_record')."
				WHERE cm_id = ".$id." and user_id = ".$user_id." order by modify_dateline desc limit 1";
		$last_cid= DB::fetch_first($sql);
		if($last_cid['ch_id']){						
			$sql = "SELECT ch_name,ch_vod_url,cc_id,cm_id,ch_id  FROM ".DB::table('course_hour')."  WHERE  ch_id = ".$last_cid['ch_id'];
			$hour_data = DB::fetch_first($sql);
						
			$ip_data=readini("ip_conf.ini");
			if($ip_data&&$hour_data['ch_vod_url']){
				$rnd_operators = array_rand($ip_data);
				$operators=$ip_data[$rnd_operators];
				if($operators){
					$rnd_ip = array_rand($operators);
					$bast_url='http://'.$operators[$rnd_ip].'/'.$hour_data['ch_vod_url'];;
					unset($operators[$rnd_ip]);
					$other_ip=$operators;
					foreach($other_ip as $o_key=>$o_val){
						$other_url[]='http://'.$o_val.'/'.$hour_data['ch_vod_url'];
					}					
				}
			}
						
		}else{
			
			//是否购买该课程
			$sql = "SELECT count(*)  FROM ".DB::table('trade_course')."  WHERE isdelete = 0 and cm_id = ".$id." and user_id = ".$user_id." and tc_flag = 2";
			$has_buy = DB::result_first($sql);			
			//已购买过，获取该课程下的一个课时
			if($has_buy){
				//选取课程下的第一个课时				
				$hour_data=$this->get_cm_data($id,$user_id,true);					
				if($hour_data){
					$bast_url=$hour_data['best_url'];
					$other_url=$hour_data['other_url'];
				}else{
					$hour_data['ch_name']='当前记录为空';
					$hour_data['ch_id'] = '-1';
					$hour_data['cm_id'] = '-1';
					$hour_data['cc_id'] = '-1';
					$bast_url=" http://60.190.243.19/vodplay/201609/12/1.1.mp4";
					$other_url=array(" http://60.190.243.12/vodplay/201609/12/1.1.mp4");
				}
			}else{//没有购买过课程
				$hour_data['ch_name']='当前记录为空';
				$hour_data['ch_id'] = '-1';
				$hour_data['cm_id'] = '-1';
				$hour_data['cc_id'] = '-1';
				$bast_url=" http://60.190.243.19/vodplay/201609/12/1.1.mp4";
				$other_url=array(" http://60.190.243.12/vodplay/201609/12/1.1.mp4");
			}						
		}
		$last_data=array(
			"ch_name"  => $hour_data['ch_name'],		
			"classTime"	=> $last_cid['look_time']?$last_cid['look_time']:0,
			"ch_id"    => $hour_data['ch_id'],
			"cm_id"   => $hour_data['cm_id'],
			"cc_id"	  => $hour_data['cc_id'],	
			"best_url" =>$bast_url,
			"other_url"=>$other_url
				
		);
			
		$cm_data=$this->get_cm_data($id,$user_id);				
		$data_result['last_play']=$last_data;
		$data_result['course_info']=$cm_data;			
		return $data_result;
	} */
	public function get_continue_detail($id,$user_id){
		if(!$id||!$user_id){
			return array();
		}
	
		$all_duration = $all_look_time = 0;
		$ch_res = $now_data = $cm_data = $ip_data= $operators = $other_ip=$other_url=array();
		$ch_operators = $other_ch_ip = $other_url=array();
		$bast_url = '';
		//获取课程信息
		$sql = "SELECT ch_id,cm_id,look_time,is_looked FROM ".DB::table('course_hour_looked')."
				WHERE cm_id = ".$id." and user_id = ".$user_id." order by modify_dateline desc limit 1";
		$last_cid= DB::fetch_first($sql);
		if($last_cid['ch_id']){
			$sql = "SELECT ch_name,ch_vod_url,cc_id,cm_id,ch_id  FROM ".DB::table('course_hour')."  WHERE  ch_id = ".$last_cid['ch_id'];
			$hour_data = DB::fetch_first($sql);
			$hour_data['is_looked']=$last_cid['is_looked'];
			/* $ip_data=readini("ip_conf.ini");
			if($ip_data&&$hour_data['ch_vod_url']){
				$rnd_operators = array_rand($ip_data);
				$operators=$ip_data[$rnd_operators];
				if($operators){
					$rnd_ip = array_rand($operators);
					$bast_url='http://'.$operators[$rnd_ip].'/'.$hour_data['ch_vod_url'];;
					unset($operators[$rnd_ip]);
					$other_ip=$operators;
					foreach($other_ip as $o_key=>$o_val){
						$other_url[]='http://'.$o_val.'/'.$hour_data['ch_vod_url'];
					}
				}
			} */
			$bast_url=$hour_data['ch_vod_url'];
			$other_url=[];
	
		}else{
				
			//是否购买该课程
			$sql = "SELECT count(*)  FROM ".DB::table('trade_course')."  WHERE isdelete = 0 and cm_id = ".$id." and user_id = ".$user_id." and tc_flag = 2";
			$has_buy = DB::result_first($sql);
			//已购买过，获取该课程下的一个课时
			if($has_buy){
				//选取课程下的第一个课时
				$hour_data=$this->get_cm_data($id,$user_id,true);
				if($hour_data){
					$bast_url=$hour_data['best_url'];
					$other_url=$hour_data['other_url'];
				}else{
					$hour_data['ch_name']='当前记录为空';
					$hour_data['ch_id'] = '-1';
					$hour_data['cm_id'] = '-1';
					$hour_data['cc_id'] = '-1';
					$bast_url=" http://60.190.243.19/vodplay/201609/12/1.1.mp4";
					$other_url=array(" http://60.190.243.12/vodplay/201609/12/1.1.mp4");
				}
			}else{//没有购买过课程
				$hour_data['ch_name']='当前记录为空';
				$hour_data['ch_id'] = '-1';
				$hour_data['cm_id'] = '-1';
				$hour_data['cc_id'] = '-1';
				$bast_url=" http://60.190.243.19/vodplay/201609/12/1.1.mp4";
				$other_url=array(" http://60.190.243.12/vodplay/201609/12/1.1.mp4");
			}
			$hour_data['is_looked'] = '0';
			
		}
		$last_data=array(
				"ch_name"  => $hour_data['ch_name'],
				"classTime"	=> $last_cid['look_time']?$last_cid['look_time']:0,
				"ch_id"    => $hour_data['ch_id'],
				"cm_id"   => $hour_data['cm_id'],
				"cc_id"	  => $hour_data['cc_id'],				
				"best_url" =>$bast_url,
				"other_url"=>$other_url,
				"is_looked" => $hour_data['is_looked']?$hour_data['is_looked']:0
	
		);
			
		$cm_data=$this->get_cm_data($id,$user_id);
		$data_result['last_play']=$last_data;
		$data_result['course_info']=$cm_data;
		return $data_result;
	}
	/*
	 *  点击某个课时进入
	 *
	 *
	 * @param $id int.
	 *
	 * @return array
	 */
	/* public function get_click_hour_detail($cm_id,$ch_id,$user_id){
		
		if(!$cm_id||!$ch_id||!$user_id){
			return array();
		}
		$all_duration = $all_look_time = 0;
		$ch_res = $chapter_data = $cm_data = $ip_data= $operators = $other_ip=$other_url=array();
		$ch_operators = $other_ch_ip = $other_url=array();
		$bast_url = '';
		
									
		$sql = "SELECT cm_id,ch_id,ch_name,ch_vod_url,cc_id  FROM ".DB::table('course_hour')."  WHERE  ch_id = ".$ch_id;
		$hour_data = DB::fetch_first($sql);	
		
		$sql = "SELECT look_time  FROM ".DB::table('course_hour_record')."  WHERE  ch_id = ".$ch_id." and user_id = ".$user_id;
		$hour_record = DB::fetch_first($sql);
		
		$ip_data=readini("ip_conf.ini");
		if($ip_data&&$hour_data['ch_vod_url']){
			$rnd_operators = array_rand($ip_data);
			$operators=$ip_data[$rnd_operators];
			if($operators){
				$rnd_ip = array_rand($operators);
				$bast_url='http://'.$operators[$rnd_ip].'/'.$hour_data['ch_vod_url'];;
				unset($operators[$rnd_ip]);
				$other_ip=$operators;
				foreach($other_ip as $o_key=>$o_val){
					$other_url[]='http://'.$o_val.'/'.$hour_data['ch_vod_url'];
				}
			}
		} 
						
		$now_data=array(
				"ch_name"  => $hour_data['ch_name'],				
				"classTime"	=> $hour_record['look_time']?$hour_record['look_time']:0,
				"ch_id"    => $ch_id,
				"cm_id"   => $cm_id,
				"cc_id"   => $hour_data['cc_id'],
				"best_url" =>$bast_url,
				"other_url"=>$other_url

		);  	    
		$cm_data=$this->get_cm_data($cm_id,$user_id);
			
		$data_result['last_play']=$now_data;
		$data_result['course_info']=$cm_data;	
		return $data_result;
	} */
	public function get_click_hour_detail($cm_id,$ch_id,$user_id){
	
		if(!$cm_id||!$ch_id||!$user_id){
			return array();
		}
		$all_duration = $all_look_time = 0;
		$ch_res = $chapter_data = $cm_data = $ip_data= $operators = $other_ip=$other_url=array();
		$ch_operators = $other_ch_ip = $other_url=array();
		$bast_url = '';
	
			
		$sql = "SELECT cm_id,ch_id,ch_name,ch_vod_url,cc_id  FROM ".DB::table('course_hour')."  WHERE  ch_id = ".$ch_id;
		$hour_data = DB::fetch_first($sql);
	
		$sql = "SELECT look_time,is_looked  FROM ".DB::table('course_hour_looked')."  WHERE  ch_id = ".$ch_id." and user_id = ".$user_id;
		$hour_record = DB::fetch_first($sql);
		/* $ip_data=readini("ip_conf.ini");
		if($ip_data&&$hour_data['ch_vod_url']){
			$rnd_operators = array_rand($ip_data);
			$operators=$ip_data[$rnd_operators];
			if($operators){
				$rnd_ip = array_rand($operators);
				$bast_url='http://'.$operators[$rnd_ip].'/'.$hour_data['ch_vod_url'];;
				unset($operators[$rnd_ip]);
				$other_ip=$operators;
				foreach($other_ip as $o_key=>$o_val){
					$other_url[]='http://'.$o_val.'/'.$hour_data['ch_vod_url'];
				}
			}
		} */
		$bast_url=$hour_data['ch_vod_url'];
		$other_url=[];
		$now_data=array(
				"ch_name"  => $hour_data['ch_name'],
				"classTime"	=> $hour_record['look_time']?$hour_record['look_time']:0,
				"ch_id"    => $ch_id,
				"cm_id"   => $cm_id,
				"cc_id"   => $hour_data['cc_id'],				
				"best_url" =>$bast_url,
				"other_url"=>$other_url,
				"is_looked" => $hour_record['is_looked']?$hour_record['is_looked']:0
	
		);
		$cm_data=$this->get_cm_data($cm_id,$user_id);
			
		$data_result['last_play']=$now_data;
		$data_result['course_info']=$cm_data;
		return $data_result;
	}
	
// 	public function get_cm_data($cm_id,$user_id,$is_last=false){
// 		$all_duration = $all_look_time = 0;
// 		$ch_res = $chapter_data = $cm_data = $ip_data= $operators = $other_ip=$other_url=array();
// 		$ch_operators = $other_ch_ip = $other_url=array();
// 		$bast_url = '';
// 		$sql = "SELECT cm_id,cm_name,cm_brief,cm_hour_num FROM ".DB::table('course_main')."
// 				WHERE isdelete = 0 and cm_id = ".$cm_id;
// 		$cm_data = DB::fetch_first($sql);
// 		if($cm_data){
// 			//课程已经看完课时数
// 			$sql = "SELECT count(*)  FROM ".DB::table('course_hour_looked')."  WHERE is_looked =1 and cm_id = ".$cm_id." and user_id = ".$user_id;
// 			$is_looked = DB::result_first($sql);
// 			$cm_data['has_looked_hour'] = $is_looked;
	
// 			//该学生在此课时下观看总时间（单位：秒）
// 			$sql = "SELECT look_time  FROM ".DB::table('course_hour_looked')."  WHERE  cm_id = ".$cm_id." and user_id = ".$user_id;
// 			$look_time = DB::fetch_all($sql);
// 			if($look_time){
// 				foreach ($look_time as $l_key => $l_val){
// 					$all_look_time +=$l_val['look_time'];
// 				}
// 			}
// 			$cm_data['look_time'] = $all_look_time;
// 			//课程一级章节课时目录
// 			$sql = "SELECT cc_id,cc_name,cc_sort,cc_level FROM ".DB::table('course_chapter')."
// 					WHERE isdelete = 0 and cc_level = 1 and cm_id = ".$cm_id." order by cc_sort ";
// 			$chapter_data = DB::fetch_all($sql);
// 			if($chapter_data){
// 				foreach ($chapter_data as $cd_key=>$cd_val){
// 					$sql = "SELECT cc_id,cc_name,cc_sort,cc_level FROM ".DB::table('course_chapter')."
// 							WHERE isdelete = 0 and cc_level = 2 and cm_id = ".$cm_id." and cc_pid = ".$cd_val['cc_id']."
// 							order by cc_sort ";
// 					$chapter_data[$cd_key]["sec_chapter"]= DB::fetch_all($sql);
	
// 				}
	
// 			}
	
// 			$cm_data['chapter_list']=$this->get_chapter_hour_list($cm_id,$user_id,$chapter_data,$is_last );
// 			if($is_last ){//返回课程下的第一课时
// 				return $cm_data['chapter_list'];
// 			}
// 		}
	
// 		return $cm_data;
	
// 	}
		
// 	public function get_cm_data($cm_id,$user_id,$is_last=false){
// 		$all_duration = $all_look_time = 0;
// 		$ch_res = $chapter_data = $cm_data = $ip_data= $operators = $other_ip=$other_url=array();
// 		$ch_operators = $other_ch_ip = $other_url=array();
// 		$bast_url = '';
// 		$sql = "SELECT cm_id,cm_name,cm_brief,cm_hour_num FROM ".DB::table('course_main')."
// 				WHERE isdelete = 0 and cm_id = ".$cm_id;
// 		$cm_data = DB::fetch_first($sql);
// 		if($cm_data){
// 			//课程已经看完课时数
// 			$sql = "SELECT count(*)  FROM ".DB::table('course_hour_record')."  WHERE is_looked =1 and cm_id = ".$cm_id." and user_id = ".$user_id;
// 			$is_looked = DB::result_first($sql);
// 			$cm_data['has_looked_hour'] = $is_looked;
	
// 			//该学生在此课时下观看总时间（单位：秒）
// 			$sql = "SELECT look_time  FROM ".DB::table('course_hour_record')."  WHERE  cm_id = ".$cm_id." and user_id = ".$user_id;
// 			$look_time = DB::fetch_all($sql);
// 			if($look_time){
// 				foreach ($look_time as $l_key => $l_val){
// 					$all_look_time +=$l_val['look_time'];
// 				}
// 			}
// 			$cm_data['look_time'] = $all_look_time;
// 			//课程一级章节课时目录
// 			$sql = "SELECT cc_id,cc_name,cc_sort,cc_level FROM ".DB::table('course_chapter')."
// 					WHERE isdelete = 0 and cc_level = 1 and cm_id = ".$cm_id." order by cc_sort ";
// 			$chapter_data = DB::fetch_all($sql);
// 			if($chapter_data){
// 				foreach ($chapter_data as $cd_key=>$cd_val){
// 					$sql = "SELECT cc_id,cc_name,cc_sort,cc_level FROM ".DB::table('course_chapter')."
// 							WHERE isdelete = 0 and cc_level = 2 and cm_id = ".$cm_id." and cc_pid = ".$cd_val['cc_id']."
// 							order by cc_sort ";
// 					$chapter_data[$cd_key]["sec_chapter"]= DB::fetch_all($sql);
						
// 				}
	
// 			}
				
// 			$cm_data['chapter_list']=$this->get_chapter_hour_list($cm_id,$user_id,$chapter_data,$is_last );
// 			if($is_last ){//返回课程下的第一课时				
// 				return $cm_data['chapter_list'];
// 			}
// 		}
	
// 		return $cm_data;
		
// 	}
	
	public function get_cm_data($cm_id,$user_id,$is_last=false){
		$all_duration = $all_look_time = 0;
		$ch_res = $chapter_data = $cm_data = $ip_data= $operators = $other_ip=$other_url=array();
		$ch_operators = $other_ch_ip = $other_url=array();
		$bast_url = '';
		$sql = "SELECT cm_id,cm_name,cm_brief,cm_hour_num FROM ".DB::table('course_main')."
				WHERE isdelete = 0 and cm_id = ".$cm_id;
		$cm_data = DB::fetch_first($sql);
		if($cm_data){
			//课程已经看完课时数
			$sql = "SELECT count(*)  FROM ".DB::table('course_hour_looked')."  WHERE is_looked =1 and cm_id = ".$cm_id." and user_id = ".$user_id;
			$is_looked = DB::result_first($sql);
			$cm_data['has_looked_hour'] = $is_looked;
	
			//该学生在此课时下观看总时间（单位：秒）
			$sql = "SELECT look_time  FROM ".DB::table('course_hour_looked')."  WHERE  cm_id = ".$cm_id." and user_id = ".$user_id;
			$look_time = DB::fetch_all($sql);
			if($look_time){
				foreach ($look_time as $l_key => $l_val){
					$all_look_time +=$l_val['look_time'];
				}
			}
			$cm_data['look_time'] = $all_look_time;
			//课程一级章节课时目录
			$sql = "SELECT cc_id,cc_name,cc_sort,cc_level FROM ".DB::table('course_chapter')."
					WHERE isdelete = 0 and cc_level = 1 and cm_id = ".$cm_id." order by cc_sort ";
			$chapter_data = DB::fetch_all($sql);
			if($chapter_data){
				foreach ($chapter_data as $cd_key=>$cd_val){
					$sql = "SELECT cc_id,cc_name,cc_sort,cc_level FROM ".DB::table('course_chapter')."
							WHERE isdelete = 0 and cc_level = 2 and cm_id = ".$cm_id." and cc_pid = ".$cd_val['cc_id']."
							order by cc_sort ";
					$chapter_data[$cd_key]["sec_chapter"]= DB::fetch_all($sql);
	
				}
	
			}
	
			$cm_data['chapter_list']=$this->get_chapter_hour_list($cm_id,$user_id,$chapter_data,$is_last );
			if($is_last ){//返回课程下的第一课时
				return $cm_data['chapter_list'];
			}
		}
	
		return $cm_data;
	
	}
	/* public function get_chapter_hour_list($cm_id,$user_id,$chapter_data,$is_last){
		$chapter_list=$ch_res=$ch_sec_res=array();
		if($chapter_data){
			$i = 1;
			foreach ($chapter_data as $cd_key=>$cd_val){
				$chapter_detail['id']=$cd_val['cc_id'];
				$chapter_detail['name']=$cd_val['cc_name'];
				$chapter_detail['cc_sort']=$cd_val['cc_sort'];			    
				$ch_res=$this->get_hour_list($cm_id,$cd_val['cc_id'],$user_id);	
				if($i == 1&&$ch_res&&$is_last){
					$last_play = $ch_res;
					if($last_play&&!empty($last_play)){
						$i++;    					
						return $last_play['0'];		
						
					}
				}
				$chapter_detail['hour_list']=$ch_res;				
				//二级章节课时列表
			    if(isset($cd_val['sec_chapter'])&&!empty($cd_val['sec_chapter'])){			    	
					foreach ($cd_val['sec_chapter'] as $cd_sec_key=>$cd_sec_val){					
						$chapter_detail['sec_chapter'][$cd_sec_key]['id']=$cd_sec_val['cc_id'];
						$chapter_detail['sec_chapter'][$cd_sec_key]['name']=$cd_sec_val['cc_name'];
						$chapter_detail['sec_chapter'][$cd_sec_key]['cc_sort']=$cd_sec_val['cc_sort'];						
						$ch_sec_res=$this->get_hour_list($cm_id,$cd_sec_val['cc_id'],$user_id);						
						$chapter_detail['sec_chapter'][$cd_sec_key]['hour_list']=$ch_sec_res;
					}
				}else{
					$chapter_detail['sec_chapter']=array();
				}
				$chapter_list[]=$chapter_detail;
			}
		}
		return $chapter_list;
	} */
	public function get_chapter_hour_list($cm_id,$user_id,$chapter_data,$is_last){
		$chapter_list=$ch_res=$ch_sec_res=array();
		if($chapter_data){
			$i = 1;
			foreach ($chapter_data as $cd_key=>$cd_val){
				$chapter_detail['id']=$cd_val['cc_id'];
				$chapter_detail['name']=$cd_val['cc_name'];
				$chapter_detail['cc_sort']=$cd_val['cc_sort'];
				$ch_res=$this->get_hour_list($cm_id,$cd_val['cc_id'],$user_id);
				if($i == 1&&$ch_res&&$is_last){
					$last_play = $ch_res;
					if($last_play&&!empty($last_play)){
						$i++;
						return $last_play['0'];
	
					}
				}
				$chapter_detail['hour_list']=$ch_res;
				//二级章节课时列表
				if(isset($cd_val['sec_chapter'])&&!empty($cd_val['sec_chapter'])){
					foreach ($cd_val['sec_chapter'] as $cd_sec_key=>$cd_sec_val){
						$chapter_detail['sec_chapter'][$cd_sec_key]['id']=$cd_sec_val['cc_id'];
						$chapter_detail['sec_chapter'][$cd_sec_key]['name']=$cd_sec_val['cc_name'];
						$chapter_detail['sec_chapter'][$cd_sec_key]['cc_sort']=$cd_sec_val['cc_sort'];
						$ch_sec_res=$this->get_hour_list($cm_id,$cd_sec_val['cc_id'],$user_id);
						$chapter_detail['sec_chapter'][$cd_sec_key]['hour_list']=$ch_sec_res;
					}
				}else{
					$chapter_detail['sec_chapter']=array();
				}
				$chapter_list[]=$chapter_detail;
			}
		}
		return $chapter_list;
	}
	/* public function get_hour_list($cm_id,$cc_id,$user_id){
		$ch_res=array();
		$sql = "SELECT * FROM ".DB::table('course_hour')."
				WHERE isdelete = 0 and cc_id = ".$cc_id." and cm_id = ".$cm_id;
		$ch_data = DB::fetch_all($sql);
		$ch_res=$sort=array();
		if($ch_data){			
			foreach ($ch_data as $c_key => $c_val){
				$other_ch_url=array();
				$all_duration +=$c_val['ch_duration'];
				$ch_info['ch_id'] = $c_val['ch_id'];
				$ch_info['cm_id'] = $cm_id;
				$ch_info['cc_id'] = $cc_id;
				$ch_info['ch_name'] = $c_val['ch_name'];
		        if($c_val['ch_start_time']<TIMESTAMP||$c_val['ch_start_time']==''){
		        	$ch_info['is_open'] ="1";		        	
		        }else{
		        	$ch_info['is_open'] ="0";
		        }
				$sql = "SELECT is_looked  FROM ".DB::table('course_hour_record')."  WHERE  ch_id = ".$c_val['ch_id']." and user_id = ".$user_id;
				$has_look = DB::fetch_first($sql);
				$ch_info['is_looked'] = $has_look['is_looked']?$has_look['is_looked']:0;
		
				//课时排序编号
				$sql = "SELECT chc_sort  FROM ".DB::table('course_hour_chapter')."  WHERE  ch_id = ".$c_val['ch_id'];
				$ch_sort = DB::fetch_first($sql);
		
				$ch_info['chc_sort']=$ch_sort['chc_sort'];
				$ip_data=readini("ip_conf.ini");
				if($ip_data&&$c_val['ch_vod_url']){
					$rnd_ch_operators = array_rand($ip_data);
					$ch_operators=$ip_data[$rnd_ch_operators];
					if($ch_operators){
						$rnd_ch_ip = array_rand($ch_operators);
						$bast_ch_url='http://'.$ch_operators[$rnd_ch_ip].'/'.$c_val['ch_vod_url'];
						unset($ch_operators[$rnd_ch_ip]);
						$other_ch_ip=$ch_operators;
		
						foreach($other_ch_ip as $oc_key=>$oc_val){
							$other_ch_url[$rnd_ch_operators][]='http://'.$oc_val.'/'.$c_val['ch_vod_url'];
						}
					}
				}
				$ch_info['best_url'] =$bast_ch_url;
		
				$ch_info['other_url']=$other_ch_url[$rnd_ch_operators];
				$sort[$c_key]=$ch_info['chc_sort'];//排序
				$ch_res[]=$ch_info;
			}
			array_multisort($sort,  $ch_res);
		}
			/* $ch_res=array(
				"ch_id" => "-1",
				"cm_id" => "-1",
				"cc_id" => "-1",
				"ch_name"=>"当前记录为空",
				"is_open" =>"0",
				"is_looked"=>"0",
				"chc_sort" =>"-1",
				"best_url" =>" http://60.190.243.19/vodplay/201609/12/1.1.mp4 ",
				"other_url"=>array(
						"http://60.190.243.12/vodplay/201609/12/1.1.mp4"
				)
			); 
			
		
		return $ch_res;
	} */
	public function get_hour_list($cm_id,$cc_id,$user_id){
		$ch_res=array();
		$sql = "SELECT * FROM ".DB::table('course_hour')."
				WHERE isdelete = 0 and cc_id = ".$cc_id." and cm_id = ".$cm_id;
		$ch_data = DB::fetch_all($sql);
		$ch_res=$sort=array();
		if($ch_data){
			foreach ($ch_data as $c_key => $c_val){
				$other_ch_url=array();
				$all_duration +=$c_val['ch_duration'];
				$ch_info['ch_id'] = $c_val['ch_id'];
				$ch_info['cm_id'] = $cm_id;
				$ch_info['cc_id'] = $cc_id;
				$ch_info['ch_name'] = $c_val['ch_name'];
				$ch_info['ch_type'] = $c_val['ch_type'];
				$ch_info['ch_start_time'] = $c_val['ch_start_time'];
				if($c_val['ch_start_time']<TIMESTAMP||$c_val['ch_start_time']==''){
					$ch_info['is_open'] ="1";
				}else{
					$ch_info['is_open'] ="0";
				}
				$sql = "SELECT is_looked  FROM ".DB::table('course_hour_looked')."  WHERE  ch_id = ".$c_val['ch_id']." and user_id = ".$user_id;
				$has_look = DB::fetch_first($sql);
				$ch_info['is_looked'] = $has_look['is_looked']?$has_look['is_looked']:0;
	
				//课时排序编号
				$sql = "SELECT chc_sort  FROM ".DB::table('course_hour_chapter')."  WHERE  ch_id = ".$c_val['ch_id'];
				$ch_sort = DB::fetch_first($sql);
	
				$ch_info['chc_sort']=$ch_sort['chc_sort'];
				/* $ip_data=readini("ip_conf.ini");
				if($ip_data&&$c_val['ch_vod_url']){
					$rnd_ch_operators = array_rand($ip_data);
					$ch_operators=$ip_data[$rnd_ch_operators];
					if($ch_operators){
						$rnd_ch_ip = array_rand($ch_operators);
						$bast_ch_url='http://'.$ch_operators[$rnd_ch_ip].'/'.$c_val['ch_vod_url'];
						unset($ch_operators[$rnd_ch_ip]);
						$other_ch_ip=$ch_operators;
	
						foreach($other_ch_ip as $oc_key=>$oc_val){
							$other_ch_url[$rnd_ch_operators][]='http://'.$oc_val.'/'.$c_val['ch_vod_url'];
						}
					}
				} */
				$ch_info['best_url'] =$c_val['ch_vod_url'];
				//$ch_info['other_url']=[];
				//$ch_info['other_url']=$other_ch_url[$rnd_ch_operators];
				//$sort[$c_key]=$ch_info['chc_sort'];//排序
				$ch_res[]=$ch_info;
			}
			//array_multisort($sort,  $ch_res);
		}		
		return $ch_res;
	}
   /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $table_name string.
     * @param $page_condition array.
     *
     * @return array
     */
	public function api($table_name,$page_condition){
        $page_result = array();
        $page = $page_condition['page'];
        $limit = $page_condition['limit'];
        $perpage = $page_condition['perpage'];
        $start=(($page-1) * $perpage);
        $wheresql = $page_condition['wheresql'];
        $orderby = $page_condition['orderby'];
        $sql_info = "SELECT * FROM ".DB::table($table_name)." WHERE 1=1 ".$wheresql." ".$page_condition['orderby']." ".DB::limit($start, $limit);
        $sql_info_result = DB::fetch_all($sql_info);
        $sql_total_rows = "SELECT count(*) FROM ".DB::table($table_name)." WHERE 1=1 ".$wheresql."";
        $sql_total_rows_result = DB::result_first($sql_total_rows);
        //DEBUG 列表数据返回结构
        $page_result = array(
            //int 返回结果总数
            'total_rows' => $sql_total_rows_result,
            //array 一页数据数组
            'page_data' => $sql_info_result 
        );
        return $page_result;
	}

    /*
	*  获取表字段值
    * @param $table_name string
    * @param $key_name string
    * @param $key_id string/array
    * @param $field_name array
    *
    * @return $field_value array
	*/    
    protected static function get_field_value_by_id($table_name,$key_name,$key_id,$field_name) {
		if(is_array($key_id) && !empty($key_id)){
			$key_ids = implode('_',$key_id);
			if(!isset(self::$field_value[$key_ids])) {
				$return = DB::fetch_all('SELECT '.$key_name.','.$field_name.' FROM '.DB::table($table_name)." WHERE ".$key_name." IN (".dimplode($key_id).")");
				foreach($return AS $key => $value){
					$return_tmp[$value[$key_name]] = $value[$field_name];
					unset($return[$key]);
				}
				self::$field_value[$key_ids]=$return_tmp;
				$key_id = $key_ids;
			}else{
				$key_id = $key_ids;
			}
		}else{
			if(!isset(self::$field_value[$key_id])) {
				self::$field_value[$key_id]=DB::result_first('SELECT '.$field_name.' FROM '.DB::table($table_name)." WHERE ".$key_name."='".$key_id."' LIMIT 1");
			}
		}
		return self::$field_value[$key_id];
	}
    
	/*
	* ajax 返回信息
	*/
	public function return_json($error,$message,$output=1) {
		$return_array = array(
			"error" => $error,
			"message" => $message
		);
		if($output){
			echo json_ext($return_array);
			die;
		}else{
			return json_ext($return_array);
		}
	}
}
?>